<template>
  <div>
    <el-form
      ref="dataForm"
      :model="dataForm"
      :rules="dataRule"
      label-width="120px"
      @keyup.enter.native="dataFormSubmit()"
      :disabled="disabled"
    >
      <el-row>
        <el-col :span="24">
          <el-form-item label="任务名称" prop="apiName">
            <el-input v-model="dataForm.apiName" placeholder="任务名称" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="任务说明" prop="apiUrl">
            <el-input
              type="textarea"
              :rows="4"
              v-model="dataForm.apiUrl"
              maxlength="100"
              show-word-limit
              placeholder="任务说明"
            />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="所属数据源" prop="apiDesc">
            <el-input v-model="dataForm.apiDesc" placeholder="所属数据源" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="已同步/应同步" prop="apiType">
            <el-input v-model="dataForm.apiType" placeholder="已同步/应同步" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="运行时长" prop="apiDesc">
            <el-input v-model="dataForm.apiDesc" placeholder="运行时长" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="开始时间" prop="apiType">
            <el-input v-model="dataForm.apiType" placeholder="开始时间" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="结束时间" prop="apiDesc">
            <el-input v-model="dataForm.apiDesc" placeholder="结束时间" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="运行状态" prop="apiType">
            <el-input v-model="dataForm.apiType" placeholder="运行状态" />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="$emit('close')">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      visible: false,
      dataForm: {
        id: 0,
        apiName: "",
        apiUrl: "",
        apiDesc: "",
        apiType: "",
        enabled: "",
        createTime: "",
        executeClass: "",
        executeMethod: "",
      },
      dataRule: {
        apiName: [
          { required: true, message: "接口名称不能为空", trigger: "blur" },
        ],
        apiUrl: [
          { required: true, message: "接口url不能为空", trigger: "blur" },
        ],
        apiType: [
          { required: true, message: "接口类型不能为空", trigger: "blur" },
        ],
        enabled: [
          { required: true, message: "是否启用不能为空", trigger: "blur" },
        ],
      },
      apiTypeList: [
        {
          label: "内部接口",
          value: 0,
        },
        {
          label: "外部接口",
          value: 1,
        },
      ],
      enabledList: [
        {
          label: "启用",
          value: 1,
        },
        {
          label: "禁用",
          value: 0,
        },
      ],
      disabled: false,
    };
  },
  methods: {
    init(row, disabled) {
      // this.dataForm.id = id || 0;
      this.visible = true;
      this.disabled = disabled;
      this.dataForm = { ...row };
      this.$refs["dataForm"].resetFields();
      // this.$nextTick(() => {
      //   this.$refs["dataForm"].resetFields();
      //   if (this.dataForm.id) {
      //     this.$http({
      //       url: this.$http.adornUrl(
      //         `/sys/sysapiconfig/info/${this.dataForm.id}`
      //       ),
      //       method: "get",
      //       params: this.$http.adornParams()
      //     }).then(({ data }) => {
      //       if (data && data.code === 0) {
      //         this.dataForm.apiName = data.sysApiConfig.apiName;
      //         this.dataForm.apiUrl = data.sysApiConfig.apiUrl;
      //         this.dataForm.apiDesc = data.sysApiConfig.apiDesc;
      //         this.dataForm.apiType = data.sysApiConfig.apiType;
      //         this.dataForm.enabled = data.sysApiConfig.enabled;
      //         this.dataForm.createTime = data.sysApiConfig.createTime;
      //         this.dataForm.executeClass = data.sysApiConfig.executeClass;
      //         this.dataForm.executeMethod = data.sysApiConfig.executeMethod;
      //       }
      //     });
      //   }
      // });
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs["dataForm"].validate((valid) => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(
              `/sys/sysapiconfig/${!this.dataForm.id ? "save" : "update"}`
            ),
            method: "post",
            data: this.$http.adornData({
              id: this.dataForm.id || undefined,
              apiName: this.dataForm.apiName,
              apiUrl: this.dataForm.apiUrl,
              apiDesc: this.dataForm.apiDesc,
              apiType: this.dataForm.apiType,
              enabled: this.dataForm.enabled,
              createTime: this.dataForm.createTime,
              executeClass: this.dataForm.executeClass,
              executeMethod: this.dataForm.executeMethod,
            }),
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: "操作成功",
                type: "success",
                duration: 1500,
                onClose: () => {
                  this.visible = false;
                  this.$emit("refreshDataList");
                },
              });
            } else {
              this.$message.error(data.msg);
            }
          });
        }
      });
    },
  },
};
</script>
